<template>
  <div id="secret-key-manage">
    <el-card class="m-b-20 w-562">
      <div slot="header"> <span>注册密钥</span></div>
      <div class="h-230">
        <div class="word-wrap w-500" v-show="!isEditRegister">{{registerKey}}</div>
        <el-input v-show="isEditRegister" type="textarea" :rows="10" v-model="registerKey" placeholder=""></el-input>
      </div>
      <el-button v-show="!isEditRegister" type="primary" @click="editInfo(0)" >编辑密钥</el-button>
      <el-button v-show="isEditRegister" @click="saveInfo(0)" >保存密钥</el-button>
      <el-button v-show="isEditRegister" type="info" @click="cancelEidt(0)" >取消编辑</el-button>
    </el-card>
    <el-card class="m-b-20 m-l-30 w-562">
      <div slot="header"><span>登录密钥</span></div>
      <div class="h-230">
        <div class="word-wrap w-500" v-show="!isEditToken">{{tokenKey}}</div>
        <el-input v-show="isEditToken" type="textarea" :rows="10" v-model="tokenKey" placeholder=""></el-input>
      </div>
      <el-button v-show="!isEditToken" type="primary" @click="editInfo(1)" >编辑密钥</el-button>
      <el-button v-show="isEditToken" @click="saveInfo(1)" >保存密钥</el-button>
      <el-button v-show="isEditToken" type="info" @click="cancelEidt(1)" >取消编辑</el-button>
    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        registerKey: '',
        tokenKey: '',
        isEditRegister: false,
        isEditToken: false,
      }
    },
    methods: {
      getSecretKey() {
        this.api.get('/admin/getSecretKey')
        .then(res => {
          this.registerKey = res.data.registerKey
          this.tokenKey = res.data.tokenKey
        })
        .catch(err => {
          console.log(err)
        })
      },
      editInfo(index) {
        if (index == 0) {
          this.isEditRegister = true
        } else {
          this.isEditToken = true
        }
      },
      async saveInfo(index) {
        if (index == 0) {
          let register = await this.api.post('/admin/editSecretKey', {
            type: 0,
            key: this.registerKey
          })
          if (register.code == 0) {
            this.getSecretKey()
            this.isEditRegister = false
            this.isEditToken = false
          }
        } else {
          let token = await this.api.post('/admin/editSecretKey', {
            type: 1,
            key: this.tokenKey
          })
          if (token.code == 0) {
            this.getSecretKey()
            this.isEditRegister = false
            this.isEditToken = false
          }
        }
      },  
      cancelEidt(index) {
        if (index == 0) {
          this.isEditRegister = false
        } else {
          this.isEditToken = false
        }
        this.getSecretKey()
      }
    },
    created() {
      this.getSecretKey()
    }
  }
</script>

<style lang="scss">
  #secret-key-manage {
    display: flex;
  }
</style>